/*
 * Copyright (c) 2009 Enio Perpetuo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *  
 */
package co.za.gvi.transport;

import co.za.gvi.bind.CustomDataType;
import co.za.gvi.bind.GviSmsMessageType;
import co.za.gvi.bind.ObjectFactory;
import co.za.gvi.bind.RecipientListType;
import co.za.gvi.bind.RecipientListType.Recipient;
import co.za.gvi.smart.SmartMessage;

/**
 * This class is used to create a message and add recipients.
 * <p>
 * The <code>MessageProxy</code> is passed as parameter to 
 * <code>Transport.send(messageProxy)</code> in order to send the message.
 * <p>
 * The <code>MessageProxy</code> supports 2 levels of <code>customData</code>:
 * <ul>
 *   <li>global (child of <code>gviSmsMessage</code>)</li>
 *   <li>as a child of the <code>recipient</code> element</li>
 * </ul> 
 * <p>
 * <b>Note:</b> Because of the way the Grapevine server processes the XML files, the customData 
 * elements MAY NOT have child elements that have the same names. If elements with 
 * the same names exist on all these levels, the lower-level elements will OVERWRITE 
 * the higher-level element and you will lose data.
 * 
 * @author Enio Perpetuo
 */
public class MessageProxy {
	
	private GviSmsMessageType message;
	private ObjectFactory factory;

	/**
	 * Constructor 
	 */
	public MessageProxy() {
		this.factory = new ObjectFactory();
		this.message = factory.createGviSmsMessageType();
	}
	
	/**
	 * Get the <code>Message</code>
	 * 
	 * @return The message
	 */
	protected GviSmsMessageType getMessage() {
		return this.message;
	}
	
	/**
	 * Add a <code>Recipient</code> and a <code>CustomData </code>as child of the 
	 * <code>recipient</code> element
	 * 
	 * @param customData The custom data to be child of the recipient element
	 * @param msisdn The mobile telephone number 
	 * @param message The text message
	 */
	public void addRecipient(CustomDataType customData, String msisdn, String message){
		
		// Creates recipient
		Recipient recipient = this.factory.createRecipientListTypeRecipient();
		recipient.setMsisdn(msisdn);
		
		// Creates custom data
		if(customData != null) {
			recipient.setCustomData(customData);
		}

		// Creates recipient list
		RecipientListType recipients = this.factory.createRecipientListType();
		recipients.setMessage(message);
		recipients.getRecipient().add(recipient);

		this.message.getRecipientList().add(recipients);
	}
	
	/**
	 * Add a <code>Recipient</code>
	 * 
	 * @param msisdn The mobile telephone number 
	 * @param message The text message
	 */
	public void addRecipient(String msisdn, String message){
		addRecipient(null, msisdn, message);
	}
	
	
	/**
	 * Add a <code>Recipient</code>
	 * 
	 * @param msisdn The mobile telephone number 
	 * @param smartMessage The vcard or vcalendar message
	 */
	public void addRecipient(String msisdn, SmartMessage smartMessage){
		addRecipient(null, msisdn, message.toString());
	}
	
	
	/**
	 * Add <code>CustomData</code> at the global level
	 * 
	 * @param customData
	 */
	public void addCustomData(CustomDataType customData){
		this.message.setCustomData(customData);
	}

	/**
	 * Add <code>RecipientList</code> to the <code>Message</code>
	 * @param recipientListProxy
	 */
	public void addRecipientList(RecipientListProxy recipientListProxy) {
		this.message.getRecipientList().add(recipientListProxy.getRecipientListType());
	}
	
	/**
	 * Set the <code>TransmissionRules</code> for the message
	 * <p>
	 * <b>Note:</b> <code>Transmission Rules</code> are currently NOT supported. They are included in the API for future use.
	 * 
	 * @param transmissionRulesProxy
	 */
	public void setTransmissionRules(TransmissionRulesProxy transmissionRulesProxy) {
		this.message.setTransmissionRules(transmissionRulesProxy.getTransmissionRules());
	}
}
